package com.ruoyi.order.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.domain.JwtUtils;
import com.sun.istack.internal.NotNull;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.vo.TOrderVo;
import com.ruoyi.order.domain.bo.TOrderBo;
import com.ruoyi.order.service.ITOrderService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单
 *
 * @author ruoyi
 * @date 2022-08-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/order/order")
public class TOrderController extends BaseController {

    private final ITOrderService iTOrderService;

    /**
     * 查询订单列表
     */
    @SaCheckPermission("order:order:list")
    @GetMapping("/list")
    public TableDataInfo<TOrderVo> list(TOrderBo bo, PageQuery pageQuery) {
        return iTOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单列表
     */
    @SaCheckPermission("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TOrderBo bo, HttpServletResponse response) {
        List<TOrderVo> list = iTOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单", TOrderVo.class, response);
    }

    /**
     * 获取订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("order:order:query")
    @GetMapping("/{id}")
    public R<TOrderVo> getInfo(@NotEmpty(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iTOrderService.queryById(id));
    }

    /**
     * 新增订单
     */
    @SaCheckPermission("order:order:create")
    @RepeatSubmit()
    @PostMapping("createOrder/{courseId}")
    public R<Void> create(@PathVariable String courseId ,HttpServletRequest request) throws Exception {
        Boolean orderId = iTOrderService.createOrder(courseId,JwtUtils.getMemberIdByJwtToken(request));

        return toAjax( orderId? 1 : 0);
    }


    /**
     * 新增订单
     */
    @SaCheckPermission("order:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TOrderBo bo) {
        return toAjax(iTOrderService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改订单
     */
    @SaCheckPermission("order:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TOrderBo bo) {
        return toAjax(iTOrderService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("order:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iTOrderService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
